//
//  HXStatus.swift
//  DemoSinaWeibo
//
//  Created by Mon on 15/12/11.
//  Copyright © 2015年 Mon. All rights reserved.
//

import UIKit

class HXStatus: NSObject {
    
    /// 微博创建时间
    var created_at:	String?
    
    /// 微博ID
    var id: Int = 0
    
    /// 微博信息内容
    var text: String?
    
    /// 微博来源
    var source:	String?
    
    /// 转发数
     var reposts_count: Int = 0
    
    /// 评论数
    var comments_count: Int = 0
    
    /// 表态数(赞)
    var attitudes_count: Int = 0
    
    /// 微博缩略配图地址
    var pic_urls: [[String: AnyObject]]?
    
    /// 微博作者的用户信息字段
    var user: HXUser?
    
    /// 字典转模型
    init(dict: [String: AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    /// 当KVC字典转模型时,字典中有需要被忽略的KEY
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    /// 打印对象信息
    override var description: String {
        
        let descrip = ["created_at", "id", "text", "source", "reposts_count", "comments_count", "attitudes_count", "pic_urls", "user"];
        
        return "\n \t 微博模型:\(dictionaryWithValuesForKeys(descrip))"
    }
    
    /// 统一在本类中获取微博信息
    class func loadStatus(finished: (statuses: [HXStatus]?, error: NSError?) -> ()) {
        // 获取网络工具类单例
        HXNetWorkTool.sharedNetWorkTool.loadStatus { (result, error) -> () in
            // 判断是否有数据
            if result == nil || error != nil {
                //能到这里来说明没有加载到数据
                print("error:\(error)")
                finished(statuses: nil, error: error)
                return
            }
            
            // 加载到微博数据
            if let array = result!["statuses"] as? [[String: AnyObject]] {
                // 获取到了statuses
                
                var statuses = [HXStatus]()
                
                for dict in array {
                    let status = HXStatus(dict: dict)
                    statuses.append(status)
                }
                
                finished(statuses: statuses, error: nil)
                
            } else {
                // 返回的字典不能转模型
                finished(statuses: nil, error: nil)
            }
        }
    }
}
